package cn.tedu.jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author: Miyako
 * @Date: 2024-07-31-9:41
 * @Description: 数据库连接池
 */
public class DBUtilPool {
    private static String dB="tedu";
    private static DruidDataSource ds;
    private static int i = 0;
    static {//加载这个类之前就创建任意数量的连接对象的连接池
        ds = new DruidDataSource();
        ds.setUrl("jdbc:mysql://localhost:3306/"+dB+"?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        ds.setUsername("root");
        ds.setPassword("root");
        ds.setInitialSize(5);
        ds.setMaxActive(6);
    }
    DBUtilPool(){
    }
    DBUtilPool(String dB){
        this.dB=dB;
    }

    public static Connection getConnection () throws SQLException {
        System.out.println("已使用"+(ds.getActiveCount()+1)+"个链接");
        return ds.getConnection();//从连接池里获取
    }
}
